// 主页代码 (index.js)
import {http} from '../../utils/requestUtil'
Page({
  data: {
    searchValue:'',
    showButton: false,
    unimportantNotes:[],
    // 重要便签数据
    importantNotes: [
      {
        id: 1,
        title: "如果未加载数据请刷新重试",
        content: "如果未加载数据请刷新重试",
        time: "今天 10:30",
        tag: "工作",
        isImportant: true
      }
    ],
    
    // 普通便签数据
    normalNotes: [
      {
        id: 3,
        title: "购物清单",
        content: "牛奶、鸡蛋、面包、水果、蔬菜、鸡肉",
        time: "昨天 15:20",
        tag: "生活",
        isImportant: false
      }
    ],
    
    // 统计数据
    importantCount: 0,
    normalCount: 0
  },
  gotoDetail(e){
wx.navigateTo({
  url: '/pages/detail/detail?id='+e.currentTarget.dataset.id,
})
  },
  async finish(e){
    wx.showModal({
      title: '提示',
      content: '完成该事件',
      success: async(res) => {
        if (res.confirm) {
          const currentId = e.currentTarget.dataset.id
          const res = await http.put("/memo/note",{noteId:currentId,delFlag:'1'})
          this.loadData()
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  async break(e){
    wx.showModal({
      title: '中断该便签',
      editable:true,
      placeholderText: '请输入中断原因...',
      // content: '',
      success: async(res) => {
        const a = res.content
        if (res.confirm) {
          console.log(a)
          const currentId = e.currentTarget.dataset.id
          const res = await http.put("/memo/note",{noteId:currentId,delFlag:'2',remark:a})
          this.loadData()
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  async delete(e){
    wx.showModal({
      title: '提示',
      content: '删除该便签',
      success: async(res) => {
        if (res.confirm) {
          const currentId = e.currentTarget.dataset.id
          const res = await http.delete("/memo/note/"+currentId,)
          this.loadData()
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  async search(){
    const res =await http.get("/memo/note/userlist",{
      title:this.data.searchValue,
      content:this.data.searchValue,
      delFlag:'0'
    })
    console.log(res)
    const importance0 = res.rows.filter(item => item.importance === '0');
    const importance1 = res.rows.filter(item => item.importance === '1');
    const importance2 = res.rows.filter(item => item.importance === '2');
this.setData({ normalNotes:importance1,
  importantNotes:importance0,
  unimportantNotes:importance2 });
  },
  inputChange: function(e) {
    this.setData({
      searchValue: e.detail.value
    });
  },
  showControll() {
    this.setData({
      showButton: !this.data.showButton
    })
  },
  async onShow(){
    this.loadData()
},
  async loadData(){
    let res;
    try{
       res =await http.get("/memo/note/userlist?pageSize=1000",{
        delFlag:'0'
      })
    }catch(e){
      console.log("ecw")
    }
  //  console.log(JSON.stringify(res))
    try{
      if(res.code!=200){
        this.loadData()
        return
      }
    }catch(e){
      this.loadData()
      return
    }
    const importance0 = res.rows.filter(item => item.importance === '0');
    const importance1 = res.rows.filter(item => item.importance === '1');
    const importance2 = res.rows.filter(item => item.importance === '2');
this.setData({ normalNotes:importance1,
  importantNotes:importance0,
  unimportantNotes:importance2,
 });

  },
  
  createNote() {
    wx.navigateTo({
      url: '/pages/home/add/add',
    });
    
    wx.showToast({
      title: '新建便签',
      icon: 'none'
    });
  }
});